//    Copyright (C) Kherty.  All rights reserved.
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using OpenLS.Chart;

using OpenLS.Drawing;
using OpenLS.Spreadsheet.Internals;
using OpenLS.Spreadsheet.UI;
using OpenLS.Spreadsheet.Drawing;
using OpenLS.Spreadsheet.UI.Internals;
using OpenLS.UI;

namespace OpenLS.Spreadsheet.UI
{
    partial class WorksheetDrawingResources
    {
        public WorksheetDrawingResources()
        {
            InitializeComponent();
        }
        void OnResizablePreviewRightButtonDown (object sender, MouseButtonEventArgs mouseButtonEventArgs)
        {
            FrameworkElement e = (FrameworkElement)sender;
            AdornerLayer l = GetAdornerLayer(sender);
            Adorner[] adorners = l.GetAdorners(e);
            if (adorners != null && adorners.Length > 0)
                return;
            Worksheet worksheet = SpreadsheetElement.GetWorksheet(e);
            ContentPresenter cp = e as ContentPresenter;
            object content = null;
            Anchor anchor = null;
            if (cp != null)
            {
                anchor = (Anchor) cp.Content;
                content = anchor.Content;
                if (content is GraphicFrame)
                {
                    return;
                }
            }

            if (!WorksheetHelper.IsAllowed(p => p.Objects)(worksheet) && anchor != null && (anchor.ClientData == null || anchor.ClientData.LocksWithSheet))
                return;

            if (content is XamlElement)
            {
                ResizeAndRotateAdorner rar = new ResizeAndRotateAdorner(e);
                rar.AllowRotate = !(content is XamlElement);
                {
                    Anchor a = cp.Content as Anchor;
                    SetupResizeAndRotateEvents(rar, e, a);

                }

                l.Add(rar);
            }

        }
        void OnResizableGotFocus(object sender, RoutedEventArgs args)
        {
            FrameworkElement e = (FrameworkElement) sender;
            AdornerLayer l = GetAdornerLayer(sender);
            Adorner[] adorners = l.GetAdorners(e);
            if (adorners != null && adorners.Length > 0)
                return;
            Worksheet worksheet = SpreadsheetElement.GetWorksheet(e);
            ContentPresenter cp = e as ContentPresenter;
            object content = null;
            Anchor anchor = cp.Content as Anchor;
            if (cp != null)
            {
                anchor = cp.Content as Anchor;
                content = anchor.Content;
            }
            if (!WorksheetHelper.IsAllowed(p => p.Objects)(worksheet) && anchor !=null && (anchor.ClientData == null || anchor.ClientData.LocksWithSheet))
                return;
            if (cp != null)
            {
                if (content is GraphicFrame || (content is XamlElement && ((XamlElement)content).FrameType == AnchorFrameType.Resize))
                {
                    ResizeAdorner resizeAdorner = new ResizeAdorner(e);
                    l.Add(resizeAdorner);
                    SetupResizeEvents(resizeAdorner, e, anchor);
                    var parent = WindowServices.GetAncestor<ItemsControl>(sender);
                    SpreadsheetElement.SetSelectedAnchor(parent, anchor);
                    return;
                }
            }
            if (content is XamlElement && ((XamlElement)content).FrameType == AnchorFrameType.None)
                return;
#if DEBUG
            if (e is ContentPresenter)
            {
                ContentPresenter cp2 = (ContentPresenter) e;
                e = e;
            }
#endif
            ResizeAndRotateAdorner rar = new ResizeAndRotateAdorner(e);
            rar.AllowRotate = !(content is XamlElement) || ((XamlElement)content).FrameType == AnchorFrameType.MoveAndRotate;
            {
                Anchor a = cp.Content as Anchor;
                SetupResizeAndRotateEvents(rar, e, a);
               
            }
            if (content is Picture)
            {
                MoveAdorner a = new MoveAdorner(e) {DataContext = cp.DataContext};
                l.Add(a);
                Picture p = (Picture) content;
                rar.ProportionSize = new Size(p.DesiredWidth, p.DesiredHeight);
            }
           
            l.Add(rar);
        }

        private static void SetupResizeEvents(ResizeAdorner e, DependencyObject adornedElement, Anchor anchor)
        {
            e.DragDelta += delegate(object sender, DragDeltaEventArgs args)
                               {
                                   var worksheet = SpreadsheetElement.GetWorksheet(sender);
                                   if (!WorksheetHelper.ProtectionFunction(sh => sh.Objects)(worksheet))
                                       return;
                                   anchor.Drag(args.HorizontalChange, args.VerticalChange);
                                   args.Handled = true;
                               };
            e.DragSideDelta += delegate(object sender, DragSideDeltaEventArgs args)
                                   {
                                       var worksheet = SpreadsheetElement.GetWorksheet(sender);
                                       if (!WorksheetHelper.ProtectionFunction(sh => sh.Objects)(worksheet))
                                           return;
                                       anchor.DragSide(args.Amount, args.DragSide);
                                       args.Handled = true;

                                       UIElement parent = (UIElement)VisualTreeHelper.GetParent(adornedElement);
                                       parent.InvalidateMeasure();
                                       parent.InvalidateArrange();
                                       e.InvalidateMeasure();
                                       e.InvalidateArrange();
                                   };

        }
        private static void SetupResizeAndRotateEvents(ResizeAndRotateAdorner e, DependencyObject adornedElement, Anchor anchor)
        {
            e.DragSideDelta += delegate(object sender, DragSideDeltaEventArgs args)
            {
                anchor.DragSide(args.Amount, args.DragSide);
                args.Handled = true;

                var parent = (UIElement)VisualTreeHelper.GetParent(adornedElement);
                parent.InvalidateMeasure();
                parent.InvalidateArrange();
                e.InvalidateMeasure();
                e.InvalidateArrange();
            };

        }
        void OnObjectMouseDown(object sender, MouseButtonEventArgs mouseButtonEventArgs)
        {
            var e = (FrameworkElement)sender;
            e.Focus();
            mouseButtonEventArgs.Handled = true;
      }

        private static AdornerLayer GetAdornerLayer(object sender)
        {
            DependencyObject e = (FrameworkElement)sender;
            var v = (Visual)VisualTreeHelper.GetParent(e);
            if (v == null)
                return null;
            AdornerLayer l = AdornerLayer.GetAdornerLayer(v);
            return l;
        }
        void OnObjectKeyDown(object sender, KeyEventArgs args)
        {
            switch (args.Key)
            {
                case Key.Delete:
                    {
                        DependencyObject e = (FrameworkElement)sender;
                        IInputElement nextFocus = FocusElement.GetFocusRoot(e);
                        var cc = (ContentPresenter)sender;
                        var an = (Anchor)cc.Content;
                        an.Worksheet.Anchors.Remove(an);
                        if (nextFocus != null)
                            nextFocus.Focus();
                        args.Handled = true;
                    }
                    break;
                case Key.Escape:
                    {
                        DependencyObject e = (FrameworkElement)sender;
                        IInputElement nextFocus = FocusElement.GetFocusRoot(e);
                        if (nextFocus != null)
                            nextFocus.Focus();
                        args.Handled = true;
                    }
                    break;
            }
        }

        void OnResizableLostFocus(object sender, RoutedEventArgs args)
        {
            FrameworkElement e = (FrameworkElement)sender;
            AdornerLayer l = GetAdornerLayer(sender);
            if (l == null)
                return;
            var adorders = l.GetAdorners(e);
            if (adorders == null)
                return;
            foreach (Adorner a in adorders)
                l.Remove(a);
        }




    }
}
